package org.testmyapp;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.springframework.dao.DataAccessResourceFailureException;
import org.testmyapp.entities.Bill;
import org.testmyapp.entities.BillPosition;
import org.testmyapp.persistence.BusinessObjectPersistenceProvider;

// Leistungskomponente
@Named
@ApplicationScoped
public class BenefitComponentService {

	@Inject
	private BusinessObjectPersistenceProvider bopp;

	public List<Bill> loadIdenticalBills(Bill currentBill) {
		String sql = "select a from " + Bill.tableName
				+ " a where a.startDate = :start and a.endDate = :end and a.billSum = :sum and a.careProvider = :cp";

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("start", currentBill.getStartDate());
		params.put("end", currentBill.getEndDate());
		params.put("sum", currentBill.getBillSum());
		params.put("cp", currentBill.getCareProvider());

		try {
			List<Bill> billListFromDb = bopp.queryList(sql, params, Bill.class);
			return billListFromDb;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

	public List<BillPosition> loadBillPositionsForBill(Bill b) {
		String sql = "select a from " + BillPosition.tableName + " a where a.bill = :bill_fk";

		Map<String, Object> params = new HashMap<String, Object>();
		params.put("bill_fk", b);

		try {
			List<BillPosition> billPositionListFromDb = bopp.queryList(sql, params, BillPosition.class);
			return billPositionListFromDb;
		} catch (Exception e) {
			throw new DataAccessResourceFailureException("Couldn't load object");
		}
	}

}
